package com.example.eurekaclient.controller;


import cn.ucloud.censor.client.CensorClient;
import cn.ucloud.censor.client.DefaultCensorClient;
import cn.ucloud.censor.model.CreateUAICensorResourceParam;
import cn.ucloud.censor.pojo.CensorConfig;
import cn.ucloud.common.pojo.Account;
import cn.ucloud.common.pojo.Param;
import cn.ucloud.common.util.Signature;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.Locale;

@RestController

public class UPornController {

    private Account account;
    @Value("8084")
    @ResponseBody
    @RequestMapping(value="/uporn",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    public JSONObject getImageByJSON(@RequestParam(value = "url",required = true) String Url) throws JSONException {

        RestTemplate restTemplate = new RestTemplate();

        Long timestamp = System.currentTimeMillis();

        account = new Account("BmwQd7dRrVJHT0c83hykfI7GyOYlhtGoI5aizTGezruJEkZOz3kXYmGCH6MPITESrU","BZUNQWSDjKo6aOE7zA6tc0IhnE46BkP8Mu7nVnG5");

        Param[] params = {
                new Param("Url",Url),
                new Param("ResourceId","uaialg-qssauopg"),
                new Param("PublicKey","BmwQd7dRrVJHT0c83hykfI7GyOYlhtGoI5aizTGezruJEkZOz3kXYmGCH6MPITESrU"),
                new Param("Timestamp",timestamp)
        };

        String paramAfterSignature = Signature.stitchParams(params);
        String ucloudurl = "http://api.uai.ucloud.cn/ucloud/censor";
        //String ResourceId = "uaialg-qssauopg";
        //String PublicKey =  "BZUNQWSDjKo6aOE7zA6tc0IhnE46BkP8Mu7nVnG5";
        //long Timestamp = System.currentTimeMillis();

        String Scenes = "porn";
        String Method ="url";

        JSONObject dataobj = new JSONObject();
        dataobj.put("Scenes",Scenes);
        dataobj.put("Method",Method);
        dataobj.put("Url",Url);

        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Signature",paramAfterSignature);
        headers.add("PublicKey","BZUNQWSDjKo6aOE7zA6tc0IhnE46BkP8Mu7nVnG5");
        headers.add("ResourceId","uaialg-qssauopg");
        headers.add("Timestamp",timestamp.toString());

        HttpEntity<String> request = new HttpEntity<String>(dataobj.toString(), headers);

        return restTemplate.postForObject(ucloudurl, request,JSONObject.class);
    }


}
